package io.renren.admin;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.renren.entity.NationalFlagEntity;
import io.renren.service.NationalFlagService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;
import io.renren.utils.validator.ValidatorUtils;


/**
 * 国旗
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-25 14:31:14
 */
@RestController
@RequestMapping("nationalflag")
public class NationalFlagController {
	@Autowired
	private NationalFlagService nationalFlagService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("nationalflag:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<NationalFlagEntity> nationalFlagList = nationalFlagService.queryList(query);
		int total = nationalFlagService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(nationalFlagList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	@RequestMapping("/query")
	@RequiresPermissions("nationalflag:query")
	public R query(@RequestParam(required = false)Map<String, Object> params){

		List<NationalFlagEntity> all = nationalFlagService.query(params);
		return R.ok().put("nationalFlag", all);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("nationalflag:info")
	public R info(@PathVariable("id") Integer id){
		NationalFlagEntity nationalFlag = nationalFlagService.queryObject(id);
		
		return R.ok().put("nationalFlag", nationalFlag);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("nationalflag:save")
	public R save(@RequestBody NationalFlagEntity nationalFlag){
		ValidatorUtils.validateEntity(nationalFlag);
		nationalFlagService.save(nationalFlag);
		
		return R.ok();
	}
	
	@RequestMapping("/batch")
	@RequiresPermissions("nationalflag:batch")
	public R batch(){
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>dsss");
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("nationalflag:update")
	public R update(@RequestBody NationalFlagEntity nationalFlag){
		ValidatorUtils.validateEntity(nationalFlag);
		nationalFlagService.update(nationalFlag);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("nationalflag:delete")
	public R delete(@RequestBody Integer[] ids){
		nationalFlagService.deleteBatch(ids);
		
		return R.ok();
	}
	
	@RequestMapping("/upload")
	@RequiresPermissions("nationalflag:upload")
	public R upload(@RequestParam("file") MultipartFile file, NationalFlagEntity nationalFlag) throws Exception {
		ValidatorUtils.validateEntity(nationalFlag);
		nationalFlagService.upload(file, nationalFlag);
		return R.ok();
	}

	
}
